<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 16:54
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Common\Tools\AppVersion;
use SDXApi\Listeners\LocationLinkSourceListener;
use SDXApi\Model\HomeTemplateStencilFontModel;

/**
 * Class HomeTemplateFontService
 * @package SDXApi\Service
 */
class HomeTemplateFontService extends TemplateAbstract
{
	/**
	 * @var HomeTemplateStencilFontModel
	 */
	protected $templateStencilFontModel;

	/**
	 * HomeTemplateFontService constructor.
	 */
	public function __construct()
	{
		$this->templateStencilFontModel = new HomeTemplateStencilFontModel();
	}

	/**
	 * 处理模版内容
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	public function handle($stencil = array(), $userId = 0, $wid = 8)
	{

        $source = $this->sourceApp();
        $cacheName = 'home_template_font_id_' . $stencil['id'] . '_app_' . $source;

//		获取缓存
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->stencilFont($stencil, $userId, $wid);
			$this->cacheData($cacheName, $data, 3600);
		}

        if ((isset($data['link_data'])) && empty($data['link_data'])) {
            $data['link_data'] = new \stdClass();
        }

		return $data;
	}

	/**
	 * 处理字体模块
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	protected function stencilFont($stencil = array(), $userId = 0, $wid = 8)
	{
		$content = $this->findTemplateStencilFontByStencilId($stencil['id']);

		$data = $this->checkStencilFontType($stencil, $content);

		return $data;
	}

	/**
	 * 处理自提类型
	 * Created by Jerry
	 * @param array $stencil
	 * @param array $font
	 * @return array
	 */
	protected function checkStencilFontType($stencil = array(), $font = array())
	{
		switch ($font['type']) {
			case 0:
				$data = $this->baseFont($stencil, $font);
				break;
			case 1:
				$data = $this->notices($stencil, $font);
				break;
			case 2:
				$data = $this->title($stencil, $font);
				break;
            case 3:
                $data = $this->baseFont($stencil, $font);
                break;
			default:
				$data = array();
				break;
		}

		if (empty($data)) {
			$data = array();
		}

		return $data;
	}

	/**
	 * 处理默认字体
	 * Created by Jerry
	 * @param array $stencil
	 * @param array $font
	 * @return array|bool
	 */
	protected function baseFont($stencil = array(), $font = array())
	{
		if (empty($stencil) || empty($font)) {
			return false;
		}

		$stencil['content'] = $font['content'];
		$stencil['link'] = $font['link'];

        $locationLinkListener = new LocationLinkSourceListener();
        $link_data = $locationLinkListener->onCallLink($font['link']);

        if (! empty($link_data)) {
            $stencil['link_data'] = $link_data;
        }

		$stencil['font_type'] = $font['type'];

		return $stencil;
	}

	/**
	 * 处理公告栏类型
	 * Created by Jerry
	 * @param array $stencil
	 * @param array $font
	 * @return array
	 */
	protected function notices($stencil = array(), $font = array())
	{
		$result = preg_split('/[;\r\n]+/s', $font['content']);

		if (empty($result)) {
			$result = array();
		}

		$notices = $this->findNotices($stencil['id']);

        $locationLinkListener = new LocationLinkSourceListener();
		foreach ($notices as $key => $value) {

            $link_data = $locationLinkListener->onCallLink($value['link']);

            if (! empty($link_data)) {
                $notices[$key]['link_data'] = $link_data;
            }

        }

		if (empty($notices)) {
			$notices = array();
		}

		$stencil['notices_new'] = $notices;

		$stencil['notices'] = $result;

		$stencil['font_type'] = $font['type'];

		unset($stencil['content']);

		return $stencil;
	}

	/**
	 * 获取公告栏数据
	 * Created by Jerry
	 * @param int $id
	 * @return array
	 */
	protected function findNotices($id = 0)
	{
		$notices = $this->templateStencilFontModel->find_template_stencil_font_notice_by_stencil_id($id);

		$data = array();
		if (! empty($notices) && is_array($notices)) {
			foreach ($notices as $key => $value) {
				$data[] = $value;
			}
		}

		return $data;
	}

	/**
	 * 处理标题类型
	 * Created by Jerry
	 * @param array $stencil
	 * @param array $font
	 * @return array
	 */
	protected function title($stencil = array(), $font = array())
	{
		$stencil['font_title'] = $font['title'];
		$stencil['font_small'] = $font['small'];
		$stencil['font_type'] = $font['type'];

		if (! AppVersion::checkAppName('android')) {
            $stencil['font_link'] = $font['link'];
        }

		return $stencil;
	}

	/**
	 * 查找指定模块
	 * Created by Jerry
	 * @param $stencilId
	 * @return mixed
	 */
	protected function findTemplateStencilFontByStencilId($stencilId)
	{
		return $this->templateStencilFontModel->find_template_stencil_font_by_stencil_id($stencilId);
	}
}